
<?php
//use for change the tasks' data
@session_start();
    if(isset($_POST['type'])){
        if($_POST['type']=='U'){
            $taskID=$_POST['taskID'];
            $notes=$_POST['notes'];
            if(isset($_POST['wlus'])&&isset($_POST['unitnum'])){
                $wlus=$_POST['wlus'];
                $unitnum=$_POST['unitnum'];
            }
            else{
                $wlus=null;
                $unitnum=null;
            }
            Update($taskID,$wlus,$unitnum,$notes);
        }
        else if($_POST['type']=='F'){
            $listID=$_POST['listID']; 
            $taskID=$_POST['taskID'];
            $current=$_POST['current'];
            Finish($taskID,$current,$listID);
        }
        else if($_POST['type']=='update_task'){
            $taskID=$_POST['taskID'];
            $notes=$_POST['notes'];
            if(isset($_POST['wlus'])&&isset($_POST['unitnum'])){
                $wlus=$_POST['wlus'];
                $unitnum=$_POST['unitnum'];
            }
            else{
                $wlus=null;
                $unitnum=null;
            }
            update_task($taskID,$wlus,$unitnum,$notes);
        }
    }
    else if(isset($_POST['acad_task_request'])){
        $type=$_POST['acad_task_request'];
        acad_task_request($type);
    }
    else if(isset($_POST['insert_alc_task'])){
        $taskid=$_POST['taskid'];
        $staffname=$_POST['username'];
        insert_alc_task($taskid,$staffname);
    }
    else if(isset($_POST['creat_choose'])){
        $choose_way=$_POST['creat_choose'];
        $supertaskid=$_POST['supertask'];
        if($choose_way==1){
            if($supertaskid=='none'){
                create_task();
            }else{
                creat_show_tpl($supertaskid);
            }
        }
        else if($choose_way==2){
            $templateid=$_POST['templateid'];
            if($templateid=='none'){
                if($supertaskid=='none'){
                    create_task();
                }else{
                    creat_show_tpl($supertaskid);
                }
            }else{
                creat_show_full($templateid,$supertaskid);
            }
        }
        else if($choose_way==3){//Academic Staff choose super task
            if($supertaskid=='none'){
                acad_create_task();
            }else{
                creat_show_tpl($supertaskid);
            }
        }
        else if($choose_way==4){
            $templateid=$_POST['templateid'];
            creat_show_full($templateid,$supertaskid);
        }
    }
    else if(isset($_POST['task_request_type'])){
        $type=$_POST['task_request_type'];
        show_task_type($type);
    }
    else if(isset($_POST['task_infor_creat'])){
        $supertaskid=$_POST['taskid'];
        $templateid=$_POST['templateid'];
        $taskname=$_POST['taskname'];
        $unit_num=$_POST['unit_num'];
        $acl_wlus=$_POST['acl_wlus'];
        $modulecode=$_POST['modulecode'];
        $note=$_POST['note'];
        insert_task($supertaskid,$templateid,$modulecode,$taskname,$unit_num,$acl_wlus,$note);
    }
    else if(isset($_POST['deleteTaskshow'])){
        $taskid=$_POST['taskid'];
        delete_task_show($taskid);
    }
    else if(isset($_POST['delete_task'])){
        $taskid=$_POST['taskid'];
        delete_task($taskid);
    }
    function delete_task($taskid){
        include"sqlconnect.php";
        $sql="DELETE FROM task WHERE taskID='$taskid'";
        mysqli_query($conn,$sql);
        $sql="SELECT count(1) as num FROM task_hierarchy WHERE subTaskID=$taskid";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $num=$row['num'];
        }
        if($num>0){
            $sql="DELETE FROM task_hierarchy WHERE subTaskID='$taskid'";
            mysqli_query($conn,$sql);
        }
        $sql="SELECT count(1) as num FROM allocated_task WHERE taskID=$taskid";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $num=$row['num'];
        }
        if($num>0){
            $sql="DELETE FROM allocated_task WHERE taskID=$taskid";
            mysqli_query($conn,$sql);
        }
        mysqli_close($conn);
    }
    function delete_task_show($taskid){
        echo"<div class='modal-dialog'>
                <div class='modal-content'>
                    <div class='modal-header'>
                    <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                    <h4 class='modal-title' id='myModalLabel'>Delete Task</h4>
                    </div>";
        include "sqlconnect.php";
        $sql="SELECT count(1)as num FROM task_hierarchy WHERE taskID='$taskid'";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $num=$row['num'];
        }
        if($num>0){
            echo"<div class='modal-body'>
                    <h4>This task has subtask.It can't be deleted</h4>
                </div>   
                    <div class='modal-footer'>                   
                    <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
                    </div>    
                </div>
                </div>";
        }
        else{
            $sql="SELECT count(1) as num FROM allocated_task WHERE taskID='$taskid' and isFinished=0";
            $result=mysqli_query($conn,$sql);
            while($row=mysqli_fetch_array($result)){
                $num1=$row['num'];
            }
            if($num1>0){
                 echo"<div class='modal-body'>
                    <h4>This task has allocated.It can't be deleted</h4>
                </div>   
                    <div class='modal-footer'>                   
                    <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
                    </div>    
                </div>
                </div>";
            }else{
                $sql="SELECT * FROM task WHERE taskID='$taskid'";
                $result=mysqli_query($conn,$sql);
                while($row=mysqli_fetch_array($result)){
                    $taskName=$row['taskName'];
                }
                echo"<div class='modal-body'>
                    <h4>Are you sure you want to delete $taskName</h4>
                    <h5 class='text-danger'>The deleted data can't be recovered!!!</h5>
                </div>   
                    <div class='modal-footer'>                   
                    <button id='delete_task' type='button' class='btn btn-danger' data-dismiss='modal' data-id='$taskid'>DELETE</button>
                    </div>    
                </div>
                </div>";
            }
        }            
    }
    function acad_task_request($type){
        switch($type){
            case 'all':
                acad_show_title();
                acad_alltask_show();
                break;
            case 'alcted':
                acad_show_title();
                acad_alctedtask_show();
                break;
            case 'unalcted':
                acad_show_title();
                unalcted_show();
                break;
            case 'create_task':
                acad_create_task();
                break;
            default:
                echo '1023';
        }
    }
    function acad_create_task(){
        include 'sqlconnect.php';
        $staffname=$_SESSION['username'];
        $sql="SELECT distinct task.taskID,task.taskName FROM task,allocated_task WHERE task.actualWLUs is null 
                AND allocated_task.username='$staffname'
                AND task.taskID=allocated_task.taskID
                AND allocated_task.isFinished=0";
        $result=mysqli_query($conn,$sql);
        echo"<div class='container-fluid addstaff-form'>
                <h3 class='form-header'>New Task Information</h3>
                <form role='form' action=' method='POST'>
                    <input name='type' value='staff_C' style='display:none;'>
                    <div class='col-md-4 col-md-offset-4'>
                        <label>Please Choose a super Task</label>
                        <select id='surpertask_choose' class='form-control input-formgroup'>
                            <option value='none' selected='selected' >None</option>";
                            while($row=mysqli_fetch_array($result)){
                                $taskid=$row['taskID'];
                                $taskName=$row['taskName'];
                                echo"<option value='$taskid'>$taskName</option>";
                            }
                    echo"</select>
                        <label>Module Code</label>
                        <select id='module_choose' class='form-control input-formgroup'>
                            <option value='none' selected='selected' >None</option>
                        </select>
                    </div>
                    </form>
                    <div class='col-md-4 col-md-offset-4 '>         
                        <button id='creat_submit_btn' class='btn btn-default btn form-button'>Submit</button>
                        <button class='btn btn-default btn form-button'>Reset</button>
                    </div>                         
            </div>";
    }
    function acad_show_title(){
        echo"<div class='container-fluid template-list-item template-list-header'>
                <div class='col-md-6'>Task Name</div>
                <div class='col-md-3'>Staff</div>       
                <div class='col-md-2'>View</div>
            </div>";
    }
    function acad_alctedtask_show(){
        include 'sqlconnect.php';
        $sql="SELECT *FROM allocated_task,task 
                WHERE allocated_task.taskID=task.taskID AND allocated_task.taskID 
                not in (SELECT subTaskID FROM task_hierarchy);";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
           $levelNum=1;
           $taskid=$row['taskID'];
           $taskname=$row['taskName'];
           $sql="SELECT firstName,lastName FROM allocated_task,staff WHERE 
           staff.username=allocated_task.username AND allocated_task.taskID=$taskid";
           $result_1=mysqli_query($conn,$sql);
           while($row_1=mysqli_fetch_array($result_1)){
                $staffname=$row_1['firstName']." ".$row_1['lastName'];
           }
           echo"<div class='container-fluid template-list-item template-level-$levelNum l_$taskid'>
                <div class='col-md-6'>$taskname</div> 
                <div class='col-md-3'>$staffname</div>      
                <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='V'>View</button></div>";
           $t=mysqli_query($conn,'SELECT count(1) AS num FROM task_hierarchy WHERE taskID='.$taskid);
            while($row1=mysqli_fetch_array($t)){
                $num=$row1['num'];
            }
            if($num>0){
                echo"<div class='col-md-1'><span class='glyphicon glyphicon-triangle-left subtemplete' data-id='$taskid'></span></div>
                </div>
                <div class='$taskid' style='display:none;'>";
                    acad_show_sub_task($taskid,$levelNum);
                echo"</div>";
            }else{
                echo"</div>";
            }
        }
    }
    function acad_alltask_show(){
        include 'sqlconnect.php';
        $sql="SELECT *FROM task 
                WHERE taskID not in (SELECT subTaskID FROM task_hierarchy)";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
           $levelNum=1;
           $taskid=$row['taskID'];
           $taskname=$row['taskName'];
           $sql_1="SELECT count(*) as num FROM allocated_task,staff WHERE 
           staff.username=allocated_task.username AND allocated_task.taskID=$taskid";
           $result_1=mysqli_query($conn,$sql_1);
           while($row_1=mysqli_fetch_array($result_1)){
               $num=$row_1['num'];
               if($num>0){
                    $sql_2="SELECT firstName,lastName FROM allocated_task,staff WHERE 
                    staff.username=allocated_task.username AND allocated_task.taskID=$taskid";
                    $result_2=mysqli_query($conn,$sql_2);
                    while($row_2=mysqli_fetch_array($result_2)){
                        $staffname=$row_2['firstName']." ".$row_2['lastName'];
                    }
               }else{
                   $staffname="";
               }
           }
         
           echo"<div class='container-fluid template-list-item template-level-$levelNum l_$taskid'>
                <div class='col-md-6'>$taskname</div> 
                <div class='col-md-3'>$staffname</div>      
                <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='V'>View</button></div>";
           $t=mysqli_query($conn,'SELECT count(1) AS num FROM task_hierarchy WHERE taskID='.$taskid);
            while($row1=mysqli_fetch_array($t)){
                $num=$row1['num'];
            }
            if($num>0){
                echo"<div class='col-md-1'><span class='glyphicon glyphicon-triangle-left subtemplete' data-id='$taskid'></span></div>
                </div>
                <div class='$taskid' style='display:none;'>";
                    acad_show_sub_task($taskid,$levelNum);
                echo"</div>";
            }else{
                echo"</div>";
            }
        }
    }
    function acad_show_sub_task($taskid,$levelNum){
        include 'sqlconnect.php';
        $sql="SELECT task.taskID,taskName FROM task,task_hierarchy WHERE task.taskID=task_hierarchy.subtaskID and task_hierarchy.taskID=$taskid";
        $result=mysqli_query($conn,$sql);
        $levelNum++;
        while($row=mysqli_fetch_array($result)){
            $taskid=$row['taskID'];
            $taskname=$row['taskName'];
            $sql_1="SELECT count(*) as num FROM allocated_task,staff WHERE 
           staff.username=allocated_task.username AND allocated_task.taskID=$taskid";
           $result_1=mysqli_query($conn,$sql_1);
           while($row_1=mysqli_fetch_array($result_1)){
               $num=$row_1['num'];
               if($num>0){
                    $sql_2="SELECT firstName,lastName FROM allocated_task,staff WHERE 
                    staff.username=allocated_task.username AND allocated_task.taskID=$taskid";
                    $result_2=mysqli_query($conn,$sql_2);
                    while($row_2=mysqli_fetch_array($result_2)){
                        $staffname=$row_2['firstName']." ".$row_2['lastName'];
                    }
               }else{
                   $staffname="";
               }
           }
           echo"<div class='container-fluid template-list-item template-level-$levelNum l_$taskid'>
                <div class='col-md-6'>$taskname</div> 
                <div class='col-md-3'>$staffname</div>      
                <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='V'>View</button></div>";
           $t=mysqli_query($conn,'SELECT count(1) AS num FROM task_hierarchy WHERE taskID='.$taskid);
            while($row1=mysqli_fetch_array($t)){
                $num=$row1['num'];
            }
            if($num>0){
                echo"<div class='col-md-1'><span class='glyphicon glyphicon-triangle-left subtemplete' data-id='$taskid'></span></div>
                </div>
                <div class='$taskid' style='display:none;'>";
                    acad_show_sub_task($taskid,$levelNum);
                echo"</div>";
            }else{
                echo"</div>";
            }
        }
    }
    function insert_alc_task_1($taskid,$staffname){
        include 'sqlconnect.php';
        $sql="SELECT count(1) as num FROM allocated_task WHERE taskID=$taskid and username='$staffname'";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $num=$row['num'];
        }
        if($num>0){
            return 1;
        }
        else{
            $nowTime=date("Y-m-d H:i:s");
            $sql="INSERT INTO allocated_task(taskID,username,startTime,isFinished)
            VALUES($taskid,'$staffname','$nowTime',0)";
            mysqli_query($conn,$sql);
            return 2;
        }
        mysqli_close($conn);
    }
    function insert_alc_task($taskid,$staffname){
        include 'sqlconnect.php';
        $sql="SELECT count(1) as num FROM allocated_task WHERE taskID=$taskid and username='$staffname'";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $num=$row['num'];
        }
        if($num>0){
            echo"<div class='fail-modify'>This task has alloted the selected staff</div>";
        }
        else{
            $nowTime=date("Y-m-d H:i:s");
            $sql="INSERT INTO allocated_task(taskID,username,startTime,isFinished)
            VALUES($taskid,'$staffname','$nowTime',0)";
            mysqli_query($conn,$sql);
            echo"<div class='success-modify'>Allot successfull</div>";
        }
        mysqli_close($conn);
    }
    function insert_task($supertaskid,$templateid,$modulecode,$taskname,$unit_num,$acl_wlus,$note){
        include 'sqlconnect.php';
        $sql="SELECT *FROM task WHERE taskName='$taskname'";
        if(mysqli_fetch_array(mysqli_query($conn,$sql))){
            echo"<div class='fail-modify'>The taskname is repit</div>";
        }
        else{
            if($unit_num=='none'){
                if($modulecode=='none'){
                    $sql="INSERT INTO task(moduleCode,templateID,taskName,notes) 
                    VALUES ('',$templateid,'$taskname','$note')";
                }
                else{
                    $sql="INSERT INTO task(moduleCode,templateID,taskName,notes) 
                    VALUES ('$modulecode',$templateid,'$taskname','$note')";
                }
                mysqli_query($conn,$sql);
            }
            else{
                if($modulecode=='none'){
                    $sql="INSERT INTO task(moduleCode,templateID,taskName,unitNumber,actualWLUs,notes)
                    VALUES('',$templateid,'$taskname',$unit_num,$acl_wlus,'$note')";
                }else{
                    $sql="INSERT INTO task(moduleCode,templateID,taskName,unitNumber,actualWLUs,notes)
                    VALUES('$modulecode',$templateid,'$taskname',$unit_num,$acl_wlus,'$note')";
                }
                mysqli_query($conn,$sql);
            }
            if($supertaskid!='none'){
                $taskid=mysqli_insert_id($conn);
                $sql="INSERT INTO task_hierarchy(taskID,subTaskID)
                VALUES($supertaskid,$taskid)";
                mysqli_query($conn,$sql);
            }
            echo"<div class='success-modify'>Creat successfull!</div>";
            mysqli_close($conn);
        }
    }
    function creat_show_full($choose_templateid,$supertaskid){
        include 'sqlconnect.php';
        $idposition=$_SESSION['idposition'];
        $staffname=$_SESSION['username'];
        if($supertaskid=='none'){
            
            if($idposition==1||$idposition==2){
                $sql="SELECT * FROM task WHERE actualWLUs is  null";
            }else{
                $sql="SELECT distinct task.taskID,task.taskName,moduleCode FROM task,allocated_task WHERE task.actualWLUs is null 
                    AND allocated_task.username='$staffname'
                    AND task.taskID=allocated_task.taskID
                    AND allocated_task.isFinished=0";
            }
            $result=mysqli_query($conn,$sql);
            $sql_1="SELECT templateID,templateName FROM template WHERE templateID not in
            (SELECT subTemplateID FROM template_hierarchy)";
            $result_1=mysqli_query($conn,$sql_1);
            echo"
            <div class='container-fluid addstaff-form'>
                    <h3 class='form-header'>New Task Information</h3>
                    <form role='form' action=' method='POST'>
                        <input name='type' value='staff_C' style='display:none;'>
                        <div class='col-md-4 col-md-offset-1'>
                            <label>Please Choose a super Task</label>
                            <select id='surpertask_choose' class='form-control input-formgroup'>
                                <option value='none' >None</option>";
                                while($row=mysqli_fetch_array($result)){
                                    $taskid=$row['taskID'];
                                    $taskName=$row['taskName'];
                                    if($taskid==$supertaskid){
                                        $moduleCode=$row['moduleCode'];
                                        echo"<option value='$taskid' selected='selected'>$taskName</option>";
                                    }
                                    else{
                                        echo"<option value='$taskid'>$taskName</option>";
                                    }
                                }
                    echo"</select>
                        <label>Please Choose a Template</label>
                        <select id='template_choose' class='form-control input-formgroup'>
                            <option value='none'>None</option>";
                            while($row=mysqli_fetch_array($result_1)){
                                $templateid=$row['templateID'];
                                $templatename=$row['templateName'];
                                if($templateid==$choose_templateid){
                                    echo"<option value='$templateid' selected='selected'>$templatename</option>";
                                }
                                else{
                                    echo"<option value='$templateid'>$templatename</option>";
                                }
                            }
                    echo"</select>
                        <label>Module Code</label>
                        <select id='module_choose' class='form-control input-formgroup'>
                            <option value='none'>None</option>";
                            $sql="SELECT *FROM module";
                            $result=mysqli_query($conn,$sql);
                            while($row=mysqli_fetch_array($result)){
                                $moduleCode=$row['moduleCode'];
                                if($moduleCode!='')
                                echo"<option value='$moduleCode'>$moduleCode</option>";
                            }
                    echo"</select>
                        <label>Task Name</label>
                        <div class='input-group input-formgroup' >
                            <input id='taskname_input' type='text' class='form-control ' required='required'>
                        </div>";
                        $sql="SELECT count(1) as num FROM template_hierarchy WHERE templateID=$choose_templateid";
                        $result=mysqli_query($conn,$sql);
                        while($row=mysqli_fetch_array($result)){
                            $num=$row['num'];
                        }
                        if($num==0){
                            $sql="SELECT normalWLUs,TypeName,UnitID FROM template,unittype 
                            WHERE unittype.UnitID=template.unitType
                            and template.templateID=$choose_templateid";
                            $result=mysqli_query($conn,$sql);
                            while($row=mysqli_fetch_array($result)){
                                $unittype=$row['UnitID'];
                                $typename=$row['TypeName'];
                                $normalWLUs=$row['normalWLUs'];
                            }
                            echo"<label>Number of the ".$typename."s </label>
                                <div class='input-group input-formgroup' >
                                    <input id='unit_num' type='number' class='form-control ' required='required'>
                                </div>";
                        }
                    echo"</div>
                    <div class='col-md-4 col-md-offset-2'>";
                        if($num==0){
                            echo"<label>Actual Work Hour of a $typename</label>
                                <div class='input-group input-formgroup' >
                                    <input id='acl_wlus' type='number' class='form-control ' required='required' value='$normalWLUs'>
                                </div>";
                        }
                    echo"<label>Note:</label>
                        <div class='input-group input-formgroup'>
                            <textarea id='note' class='form-control' rows='11' placeholder='Description'></textarea>
                        </div>
                    </div>
                    </form>";
                    
                    echo"<div class='col-md-4 col-md-offset-4 '>         
                        <button id='creat_submit_btn' class='btn btn-default btn form-button'>Submit</button>
                        <button class='btn btn-default btn form-button'>Reset</button>
                    </div>                         
                
            </div>";
        }else{
            if($idposition==1||$idposition==2){
                $sql="SELECT * FROM task WHERE actualWLUs is  null";
            }else{
                $sql="SELECT distinct task.taskID,task.taskName,moduleCode FROM task,allocated_task WHERE task.actualWLUs is null 
                    AND allocated_task.username='$staffname'
                    AND task.taskID=allocated_task.taskID
                    AND allocated_task.isFinished=0";
            }
            $result=mysqli_query($conn,$sql);
            $sql_1="SELECT template.templateID,templateName FROM template,template_hierarchy 
            WHERE template.templateID=template_hierarchy.subTemplateID
            and template_hierarchy.templateID=(SELECT templateID FROM task WHERE taskID=$supertaskid)";
            $result_1=mysqli_query($conn,$sql_1);
            echo"<div class='container-fluid addstaff-form'>
                    <h3 class='form-header'>New Task Information</h3>
                    <form role='form' action=' method='POST'>
                        <input name='type' value='staff_C' style='display:none;'>
                        <div class='col-md-4 col-md-offset-1'>
                            <label>Please Choose a super Task</label>
                            <select id='surpertask_choose' class='form-control input-formgroup'>
                                <option value='none' >None</option>";
                                while($row=mysqli_fetch_array($result)){
                                    $taskid=$row['taskID'];
                                    $taskName=$row['taskName'];
                                    if($taskid==$supertaskid){
                                        $moduleCode=$row['moduleCode'];
                                        echo"<option value='$taskid' selected='selected'>$taskName</option>";
                                    }
                                    else{
                                        echo"<option value='$taskid'>$taskName</option>";
                                    }
                                }
                    echo"</select>
                        <label>Please Choose a Template</label>
                        <select id='template_choose' class='form-control input-formgroup'>
                            <option value='none'>None</option>";
                            while($row=mysqli_fetch_array($result_1)){
                                $templateid=$row['templateID'];
                                $templatename=$row['templateName'];
                                if($templateid==$choose_templateid){
                                    echo"<option value='$templateid' selected='selected'>$templatename</option>";
                                }
                                else{
                                    echo"<option value='$templateid'>$templatename</option>";
                                }
                            }
                    echo"</select>
                        <label>Module Code</label>
                        <select id='module_choose' class='form-control input-formgroup'>";
                            if($moduleCode==''){
                                echo"<option value='none'>None</option>";
                            }
                            else{
                                echo"<option value='$moduleCode' selected='selected' >$moduleCode</option>";
                            }
                    echo"</select>
                        <label>Task Name</label>
                        <div class='input-group input-formgroup' >
                            <input id='taskname_input' type='text' class='form-control ' required='required'>
                        </div>";
                        $sql="SELECT count(1) as num FROM template_hierarchy WHERE templateID=$choose_templateid";
                        $result=mysqli_query($conn,$sql);
                        while($row=mysqli_fetch_array($result)){
                            $num=$row['num'];
                        }
                        if($num==0){
                            $sql="SELECT normalWLUs,TypeName,UnitID FROM template,unittype 
                            WHERE unittype.UnitID=template.unitType
                            and template.templateID=$choose_templateid";
                            $result=mysqli_query($conn,$sql);
                            while($row=mysqli_fetch_array($result)){
                                $unittype=$row['UnitID'];
                                $typename=$row['TypeName'];
                                $normalWLUs=$row['normalWLUs'];
                            }
                            echo"<label>Number of the ".$typename."s</label>
                                <div class='input-group input-formgroup' >
                                    <input id='unit_num' type='number' class='form-control ' required='required'>
                                </div>";
                        }
                echo"</div>";
                echo"<div class='col-md-4 col-md-offset-2'>";
                        if($num==0){
                            echo"<label>Actual Work Hour of a $typename</label>
                                <div class='input-group input-formgroup' >
                                    <input id='acl_wlus' type='number' class='form-control ' required='required' value='$normalWLUs'>
                                </div>";
                        }
                echo"<label>Note:</label>
                     <div class='input-group input-formgroup'>
                        <textarea id='note' class='form-control' rows='11' placeholder='Description'></textarea>
                     </div>
                     </div>
                     </form>";
                echo"<div class='col-md-4 col-md-offset-4 '>         
                        <button id='creat_submit_btn' class='btn btn-default btn form-button'>Submit</button>
                        <button class='btn btn-default btn form-button'>Reset</button>
                    </div>                         
                
            </div>";
        }
    }
    function creat_show_tpl($supertaskid){
        include 'sqlconnect.php';
        $idposition=$_SESSION['idposition'];
        $staffname=$_SESSION['username'];
        if($idposition==1||$idposition==2){
            $sql="SELECT * FROM task WHERE actualWLUs is  null";
        }else{
            $sql="SELECT distinct task.taskID,task.taskName,moduleCode FROM task,allocated_task WHERE task.actualWLUs is null 
                AND allocated_task.username='$staffname'
                AND task.taskID=allocated_task.taskID
                AND allocated_task.isFinished=0";
        }
        $result=mysqli_query($conn,$sql);

        $sql_1="SELECT template.templateID,templateName FROM template,template_hierarchy 
        WHERE template.templateID=template_hierarchy.subTemplateID
        and template_hierarchy.templateID=(SELECT templateID FROM task WHERE taskID=$supertaskid)";
        $result_1=mysqli_query($conn,$sql_1);
        echo"<div class='container-fluid addstaff-form'>
                <h3 class='form-header'>New Task Information</h3>
                <form role='form' action=' method='POST'>
                    <input name='type' value='staff_C' style='display:none;'>
                    <div class='col-md-4 col-md-offset-4'>
                        <label>Please Choose a super Task</label>
                        <select id='surpertask_choose' class='form-control input-formgroup'>
                            <option value='none' >None</option>";
                            while($row=mysqli_fetch_array($result)){
                                $taskid=$row['taskID'];
                                $taskName=$row['taskName'];
                                if($taskid==$supertaskid){
                                    $moduleCode=$row['moduleCode'];
                                    echo"<option value='$taskid' selected='selected'>$taskName</option>";
                                }
                                else{
                                    echo"<option value='$taskid'>$taskName</option>";
                                }
                            }
                    echo"</select>
                        <label>Please Choose a Template</label>
                        <select id='template_choose' class='form-control input-formgroup'>
                            <option value='none' selected='selected' >None</option>";
                            while($row=mysqli_fetch_array($result_1)){
                                $templateid=$row['templateID'];
                                $templatename=$row['templateName'];
                                echo"<option value='$templateid'>$templatename</option>";
                            }
                    echo"</select>
                        <label>Module Code</label>
                        <select id='module_choose' class='form-control input-formgroup'>";
                            if($moduleCode==''){
                                echo"<option value='none'>None</option>";
                            }
                            else{
                                echo"<option value='$moduleCode' selected='selected' >$moduleCode</option>";
                            }
                    echo"</select>
                    </div>
                     </form>
                    <div class='col-md-4 col-md-offset-4 '>         
                        <button id='creat_submit_btn' class='btn btn-default btn form-button'>Submit</button>
                        <button class='btn btn-default btn form-button'>Reset</button>
                    </div>                         
            </div>";
    }
    function update_task($taskID,$wlus,$unitnum,$notes){
        include 'sqlconnect.php';
        if(mysqli_fetch_array(mysqli_query($conn,"SELECT *FROM task_hierarchy WHERE taskID=$taskID"))){
            $sql="UPDATE task SET notes='$notes' WHERE taskID=$taskID";
        }
        else{
            $sql="UPDATE task SET actualWLUs=$wlus,notes='$notes',unitNumber='$unitnum' WHERE taskID=$taskID";
        }
        mysqli_query($conn,$sql);
        reshow_U_task($taskID);
        mysqli_close($conn);
    }
    function reshow_U_task($taskID){
        include 'sqlconnect.php';
        $sql="SELECT *FROM task WHERE taskID=$taskID";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $taskid=$row['taskID'];
            $taskname=$row['taskName'];
            $levelNum=1;
            echo"
            <div class='col-md-5'>$taskname</div>       
            <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='V'>View</button></div>
            <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='U'>Update</button></div>
            <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='D'>Delete</button></div>";
            $t=mysqli_query($conn,'SELECT count(1) AS num FROM task_hierarchy WHERE taskID='.$taskid);
            while($row1=mysqli_fetch_array($t)){
                $num=$row1['num'];
            }
            if($num>0){
                echo"<div class='col-md-1 '><span class='glyphicon glyphicon-triangle-left subtemplete' data-id='$taskid'></span><span class='glyphicon glyphicon-ok success-coin'></span></div>
                ";
            }
            else{
                echo"<div class='col-md-1 '><span class='glyphicon glyphicon-ok success-coin'></span></div>";
            }
        }
    }
    function show_task_type($task_request_type){
        switch($task_request_type){
            case 'all':
                show_title();
                $sql="SELECT *FROM task 
                WHERE taskID not in (SELECT subTaskID FROM task_hierarchy)";
                show_task($sql);
            break;
            case 'alcted':
                show_title();
                alcted_show();
            break;
            case 'unalcted':
                show_title();
                unalcted_show();
            break;
            case 'finished':
                show_title();
                $sql="SELECT task.taskID,taskName FROM task,allocated_task 
                WHERE task.taskID=allocated_task.taskID and isFinished=1";
                show_finished_task($sql);
            break;
            case 'unfinished':
                show_title();
                show_unfinished_task();
            break;
            case 'crt_task':
                create_task();
            break;
            case 'alc_task':
                allocate_task();
            break;
            default:
                echo "error:1024";
        }
    }
    function show_message($taskid,$taskname,$levelNum){
        if($_SESSION['idposition']==1||$_SESSION['idposition']==2){
            echo"<div class='container-fluid template-list-item template-level-$levelNum'>
            <div class='col-md-5'>$taskname</div>       
            <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='V'>View</button></div>
            <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='U'>Update</button></div>
            <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='D'>Delete</button></div>";
        }else{
            echo"<div class='container-fluid template-list-item template-level-$levelNum l_$taskid'>
                <div class='col-md-6'>$taskname</div> 
                <div class='col-md-3'></div>      
                <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='V'>View</button></div>";
        }
        
    }
    function unalcted_show(){
        include'sqlconnect.php';
        $levelNum=1;
        $sql="SELECT taskID,taskName FROM task WHERE taskID not in (SELECT taskID FROM allocated_task )";
        $result_all=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result_all)){
            $taskid=$row['taskID'];
            $taskname=$row['taskName'];
            $sql="SELECT count(1) as num FROM task_hierarchy WHERE subTaskID=$taskid";
            $result_1=mysqli_query($conn,$sql);
            while($row_1=mysqli_fetch_array($result_1)){
                $num=$row_1['num'];
            }
            if($num==0){
                show_message($taskid,$taskname,$levelNum);
                unalcted_sub_show($taskid,$levelNum); 
            }
            else{
                $sql="SELECT count(1) as num FROM task_hierarchy WHERE 
                subTaskID=$taskid and
                taskID not in (SELECT taskID FROM allocated_task)";
                $result_2=mysqli_query($conn,$sql);
                while($row_2=mysqli_fetch_array($result_2)){
                    $num_1=$row_2['num'];
                }
                if($num_1>0){
                    continue;
                }else{
                    show_message($taskid,$taskname,$levelNum);
                    unalcted_sub_show($taskid,$levelNum);
                }
            }
        }
    }
    function unalcted_sub_show($taskid,$levelNum){
        include 'sqlconnect.php';
        $levelNum++;
        $sql="SELECT count(1) as num FROM task_hierarchy,allocated_task WHERE allocated_task.taskID=task_hierarchy.subTaskID and task_hierarchy.taskID=$taskid";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $num=$row['num'];
        }
        $sql="SELECT count(1) as num FROM task_hierarchy WHERE taskID=$taskid";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $num2=$row['num'];
        }
        if($num!=$num2){//this task has subtasks aren't in allocated_task
            echo"<div class='col-md-1 '><span class='glyphicon glyphicon-triangle-left subtemplete' data-id='$taskid'></span></div>
                </div>";
            $sql="SELECT task.taskID,taskName FROM task,task_hierarchy WHERE task.taskID=task_hierarchy.subTaskID 
            and task_hierarchy.taskID=$taskid 
            and task_hierarchy.subTaskID not in (SELECT taskID FROM allocated_task)";
            $result_1=mysqli_query($conn,$sql);
            echo"<div class='$taskid' style='display:none;'>";
            while($row=mysqli_fetch_array($result_1)){
                $subtaskid=$row['taskID'];
                $taskname=$row['taskName'];
                show_message($subtaskid,$taskname,$levelNum);
                unalcted_sub_show($subtaskID,$levelNum);
            }
            echo"</div>";
        }
        else{
            echo"</div>";
        }
    }
    function alcted_show(){
        include'sqlconnect.php';
        $sql="SELECT task.taskID,taskName FROM task,allocated_task WHERE task.taskID=allocated_task.taskID";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $taskid=$row['taskID'];
            $taskname=$row['taskName'];
            $levelNum=1;
            $sql="SELECT count(1) as num FROM task,task_hierarchy 
            WHERE task.taskID=task_hierarchy.subTaskID and task_hierarchy.subTaskID=$taskid";
            $result_1=mysqli_query($conn,$sql);
            while($row_1=mysqli_fetch_array($result_1)){
                $num=$row_1['num'];
                if($num==0){//this task doesn't have supertask'
                    show_message($taskid,$taskname,$levelNum);
                    alcted_sub_show($taskid,$levelNum);
                }
                else{
                    $sql="SELECT count(1) as num FROM allocated_task,task_hierarchy WHERE 
                    allocated_task.taskID=task_hierarchy.taskID
                    and task_hierarchy.subTaskID=$taskid";
                    $result_2=mysqli_query($conn,$sql);
                    while($row_2=mysqli_fetch_array($result_2)){
                        $num_1=$row_2['num'];
                    }
                    if($num_1>0){
                        continue;
                    }
                    else{
                        show_message($taskid,$tasknam,$levelNum);
                        alcted_sub_show($taskid,$levelNum);
                    }
                }
            }
        }
    }
    function alcted_sub_show($taskid,$levelNum){
        include'sqlconnect.php';
        $levelNum++;
        $sql="SELECT count(1) as num FROM task_hierarchy,allocated_task 
        WHERE task_hierarchy.subTaskID=allocated_task.taskID 
        and task_hierarchy.taskID=$taskid";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $num=$row['num'];
        }
        if($num>0){//$taskid has subtask in allocated_task
            echo"<div class='col-md-1 '><span class='glyphicon glyphicon-triangle-left subtemplete' data-id='$taskid'></span></div>
                </div>";
            $sql="SELECT task.taskID,taskName FROM task,allocated_task,task_hierarchy WHERE 
            task.taskID=allocated_task.taskID and allocated_task.taskID=task_hierarchy.subTaskID and task_hierarchy.taskID=$taskid";
            $result_1=mysqli_query($conn,$sql);
            echo"<div class='$taskid' style='display:none;'>";
            while($row_1=mysqli_fetch_array($result_1)){
                $subtaskID=$row_1['taskID'];
                $taskname=$row_1['taskName'];
                show_message($subtaskID,$taskname,$levelNum);
                alcted_sub_show($subtaskID,$levelNum);
            }
            echo"</div>";
        }else{
            echo"</div>";
        }
    }
    function allocate_task(){
        include 'sqlconnect.php';
        $sql="SELECT *FROM task WHERE taskID not in(SELECT taskID FROM allocated_task)";
        $result=mysqli_query($conn,$sql);
        $sql_2="SELECT username,firstname,lastname FROM staff";
        $result_2=mysqli_query($conn,$sql_2);
        echo"<div class='container-fluid addstaff-form'>
                <h3 class='form-header'>Task Allocation</h3>
                <form role='form' action='' method='POST'>
                    <div class='col-md-4 col-md-offset-4'>
                        <label>Please Choose a Task</label>
                        <input id='staff_num' value='1' style='display:none'></input>
                        <select id='taskid' class='form-control input-formgroup'>";
                        while($row=mysqli_fetch_array($result)){
                             $taskid=$row['taskID'];
                             $taskname=$row['taskName'];
                             echo"<option value='$taskid'>$taskname</option>>";
                        }
                        echo"</select>
                        </br>
                        <label>Please Choose a Staff</label>
                        <select id='username' class='form-control input-formgroup'>";
                        while($row=mysqli_fetch_array($result_2)){
                            $staffname=$row['username'];
                            $firstname=$row['firstname'];
                            $lastname=$row['lastname'];
                            echo"<option value='$staffname'>$firstname $lastname</option>>";
                        }
                        echo"</select>
                            </div>
                        </form>
                         <div class='col-md-4 col-md-offset-4'>
                            <button id='alc_cfm_btn' class='btn btn-default btn allocate-btn btn-block'>Confirm</button>
                        </div>
                    
                
            </div>";
    }
    function create_task(){
        include 'sqlconnect.php';
        $sql="SELECT *FROM task WHERE actualWLUs is null";
            $result=mysqli_query($conn,$sql);
        $sql_1="SELECT templateID,templateName FROM template WHERE templateID not in
            (SELECT subTemplateID FROM template_hierarchy)";
        $result_1=mysqli_query($conn,$sql_1);
        echo"<div class='container-fluid addstaff-form'>
                <h3 class='form-header'>New Task Information</h3>
                <form role='form' action=' method='POST'>
                    <input name='type' value='staff_C' style='display:none;'>
                    <div class='col-md-4 col-md-offset-4'>
                        <label>Please Choose a super Task</label>
                        <select id='surpertask_choose' class='form-control input-formgroup'>
                            <option value='none' selected='selected' >None</option>";
                            while($row=mysqli_fetch_array($result)){
                                $taskid=$row['taskID'];
                                $taskName=$row['taskName'];
                                echo"<option value='$taskid'>$taskName</option>";
                            }
                    echo"</select>
                        <label>Please Choose a Template</label>
                        <select id='template_choose' class='form-control input-formgroup'>
                            <option value='none' selected='selected' >None</option>";
                            while($row=mysqli_fetch_array($result_1)){
                                $templateid=$row['templateID'];
                                $templatename=$row['templateName'];
                                echo"<option value='$templateid'>$templatename</option>";
                            }
                    echo"</select>
                        <label>Module Code</label>
                        <select id='module_choose' class='form-control input-formgroup'>
                            <option value='none' selected='selected' >None</option>
                        </select>
                    </div>
                    </form>
                    <div class='col-md-4 col-md-offset-4 '>         
                        <button id='creat_submit_btn' class='btn btn-default btn form-button'>Submit</button>
                        <button class='btn btn-default btn form-button'>Reset</button>
                    </div>                         
            </div>";
    }
    function show_unfinished_task(){
        include 'sqlconnect.php';
        $sql="SELECT task.taskID,taskName FROM task,allocated_task
                WHERE task.taskID=allocated_task.taskID and isFinished=0 and task.taskID not in (SELECT subtaskID FROM task_hierarchy)";
                $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $taskid=$row['taskID'];
            $taskname=$row['taskName'];
            $levelNum=1;
            echo"<div class='container-fluid template-list-item template-level-$levelNum'>
            <div class='col-md-5'>$taskname</div>       
            <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='V'>View</button></div>
            <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='U'>Update</button></div>
            <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='D'>Delete</button></div>";
            $sql="SELECT count(1) AS num FROM task_hierarchy,allocated_task
            WHERE allocated_task.taskID=task_hierarchy.subtaskID
            and task_hierarchy.taskID=$taskid
            and isFinished=0";
            $t=mysqli_query($conn,$sql);
            while($row1=mysqli_fetch_array($t)){
                $num=$row1['num'];
            }
            if($num>0){
                echo"<div class='col-md-1 '><span class='glyphicon glyphicon-triangle-left subtemplete' data-id='$taskid'></span></div>
                </div>
                <div class='$taskid' style='display:none;'>";
                    show_unfinished_sub_task($taskid,$levelNum);
                echo"</div>";
            }
            else{
                echo"</div>";
            }
        }
        
    }
    function show_unfinished_sub_task($taskid,$levelNum){
        include 'sqlconnect.php';
        $sql="SELECT task.taskID,taskName FROM task,task_hierarchy,allocated_task 
                           WHERE task.taskID=task_hierarchy.subtaskID and task.taskID=allocated_task.taskID
                           and isFinished=0
                           and task_hierarchy.taskID=$taskid";
        $result=mysqli_query($conn,$sql);
        $levelNum++;
        while($row=mysqli_fetch_array($result)){
            $taskid=$row['taskID'];
            $taskname=$row['taskName'];
            echo"<div class='container-fluid template-list-item template-level-$levelNum'>
            <div class='col-md-5'>$taskname</div>       
            <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='V'>View</button></div>
            <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='U'>Update</button></div>
            <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='D'>Delete</button></div>";
            $sql="SELECT count(1) AS num FROM task_hierarchy,allocated_task
            WHERE allocated_task.taskID=task_hierarchy.subtaskID
            and task_hierarchy.taskID=$taskid
            and isFinished=0";
            $t=mysqli_query($conn,$sql);
            while($row1=mysqli_fetch_array($t)){
                $num=$row1['num'];
            }
            if($num>0){
                echo"<div class='col-md-1 '><span class='glyphicon glyphicon-triangle-left subtemplete' data-id='$taskid'></span></div>
                </div>
                <div class='$taskid' style='display:none;'>";
                    show_unfinished_sub_task($taskid,$levelNum);
                echo"</div>";
            }
            else{
                echo"</div>";
            }
        }
    }
    function show_finished_task($sql){
        include 'sqlconnect.php';
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $taskid=$row['taskID'];
            $taskname=$row['taskName'];
            judge_finished_show($taskid);
        }
    }
    function judge_finished_show($taskid){
        include 'sqlconnect.php';
        $sql="SELECT count(1) as num FROM allocated_task,task_hierarchy 
            WHERE allocated_task.taskID=task_hierarchy.taskID and task_hierarchy.subTaskID=$taskid";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $num=$row['num'];
        }
        $sql="SELECT allocated_task.taskID,isFinished FROM allocated_task,task_hierarchy 
            WHERE allocated_task.taskID=task_hierarchy.taskID and task_hierarchy.subTaskID=$taskid";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $superID=$row['taskID'];
            $isFinished=$row['isFinished'];
        }
        if($num>0 && $isFinished==1){
            return;
        }
        else if($num=0||$isFinished==0){
            $sql="SELECT *FROM task WHERE taskID=$taskid";
            show_task($sql);
        }
    }
    function show_title(){
        echo"<div class='container-fluid template-list-item template-list-header'>
            <div class='col-md-4'>Task Name</div>
            <div class='col-md-2'>Staff Name</div>
            <div class='col-md-2'>WLUs</div>      
            <div class='col-md-1'>View</div>
            <div class='col-md-1'>Update</div>
            <div class='col-md-1'>Delete</div>
        </div>";
    }
    function show_task_cp(){
        include 'sqlconnect.php';
        $sql="SELECT *FROM task WHERE taskID not in(SELECT subTaskID FROM task_hierarchy)";
        $result=mysqli_query($conn,$sql);
        while($row=msyqli_fetch_array($result)){
            $taskid=$row['taskID']; 
            $unitnum=$row['unitNumber'];
            $aclwlu=$row['actualWLUs'];
            if($aclwlu==null){
                
            } 
        }
    }
    function show_task($sql){
        include 'sqlconnect.php';
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $taskid=$row['taskID'];
            $taskname=$row['taskName'];
            $levelNum=1;
            echo"<div class='container-fluid template-list-item template-level-$levelNum l_$taskid'>
            <div class='col-md-4'>$taskname</div>
            <div class='col-md-2'>$taskname</div>
            <div class='col-md-2'>$taskname</div>
            <div class='col-md-1'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='V'>View</button></div>
            <div class='col-md-1'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='U'>Update</button></div>
            <div class='col-md-1'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='D'>Delete</button></div>";
            $t=mysqli_query($conn,'SELECT count(1) AS num FROM task_hierarchy WHERE taskID='.$taskid);
            while($row1=mysqli_fetch_array($t)){
                $num=$row1['num'];
            }
            if($num>0){
                echo"<div class='col-md-1 '><span class='glyphicon glyphicon-triangle-left subtemplete' data-id='$taskid'></span></div>
                </div>
                <div class='$taskid' style='display:none;'>";
                    show_sub_task($taskid,$levelNum);
                echo"</div>";
            }
            else{
                echo"</div>";
            }
        }
    }
    function show_sub_task($taskid,$levelNum){
        include 'sqlconnect.php';
         $sql="SELECT task.taskID,taskName FROM task,task_hierarchy WHERE task.taskID=task_hierarchy.subtaskID and task_hierarchy.taskID=$taskid";
        $result=mysqli_query($conn,$sql);
        $levelNum++;
        while($row=mysqli_fetch_array($result)){
            $taskid=$row['taskID'];
            $taskname=$row['taskName'];
            echo"<div class='container-fluid template-list-item template-level-$levelNum l_$taskid'>
            <div class='col-md-5'>$taskname</div>       
            <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='V'>View</button></div>
            <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='U'>Update</button></div>
            <div class='col-md-2'><button class='btn btn-default btn-sm jm-box' data-id='$taskid' data-type='D'>Delete</button></div>";
            $t=mysqli_query($conn,'SELECT count(1) AS num FROM task_hierarchy WHERE taskID='.$taskid);
            while($row1=mysqli_fetch_array($t)){
                $num=$row1['num'];
            }
            if($num>0){
                echo"<div class='col-md-1 '><span class='glyphicon glyphicon-triangle-left subtemplete' data-id='$taskid'></span></div>
                </div>
                <div class='$taskid' style='display:none;'>";
                    show_sub_task($taskid,$levelNum);
                echo"</div>";
            }
            else{
                echo"</div>";
            }
        }
    }
    function Update($taskID,$wlus,$unitnum,$notes){
        include 'sqlconnect.php';
        if(isset($wlus)&&isset($unitnum)){
            $sql="UPDATE task SET actualWLUs='$wlus',unitNumber='$unitnum',notes='$notes' WHERE taskID='$taskID'";
        }
        else{
            $sql="UPDATE task SET notes='$notes' WHERE taskID='$taskID'";
        }
        mysqli_query($conn,$sql);
        reshow_U($taskID);
        mysqli_close($conn);
    }
    function add_staff_wlu($taskID){
        include 'sqlconnect.php';
        $sql="SELECT unitNumber,actualWLUs FROM task WHERE taskID=$taskID";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $unitnum=$row['unitNumber'];
            $actwlu=$row['actualWLUs'];
        }
        $staffname=$_SESSION['username'];
        $sql="SELECT totalWLUs FROM staff WHERE username='$staffname'";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $totalwlu=$row['totalWLUs'];
        }
        $totalwlu=$totalwlu+($actwlu*$unitnum);
        $sql="UPDATE staff SET totalWLUs=$totalwlu WHERE username='$staffname'";
        mysqli_query($conn,$sql);
        mysqli_close($conn);
    }
    function Finish($taskID,$current,$listID){
        include 'sqlconnect.php';
        $sql="UPDATE allocated_task SET endTime='$current',isFinished=1
         WHERE taskID='$taskID'";
        mysqli_query($conn,$sql);
        mysqli_close($conn);
        add_staff_wlu($taskID);
        super_finished($taskID,$current);
        if($taskID==$listID){
            reshow_F_sub($taskID);
        }
        else if($listID=='q'){
            reshow_U($taskID);
        }
        else{
            reshow_F_all($taskID);
        }
    }
    function reshow_F_sub($taskID){
         echo"<div class='table-responsive'>
                <table class='table table-striped home-task-table'>
                     <thead>
                    <tr>
                        <th>Task Name</th>
                        <th>Total Work Hours</th>
                        <th>Update</th>
                        <th>Status</th>
                        <th>View</th>
                    </tr>
                    </thead>
                    <tbody>";
        reshow_U($taskID);
        echo"</tbody>
            </table>
            </div>";
    }
    function reshow_F_all($taskID){
        include 'sqlconnect.php';
        $sql="SELECT taskID FROM task_hierarchy WHERE subTaskID='$taskID'";
        if(mysqli_fetch_array(mysqli_query($conn,$sql))){//如果存在母任务
            echo"<div class='table-responsive'>
                <table class='table table-striped home-task-table'>
                     <thead>
                    <tr>
                        <th>Task Name</th>
                        <th>Total Work Hours</th>
                        <th>Update</th>
                        <th>Status</th>
                        <th>View</th>
                    </tr>
                    </thead>
                    <tbody>";
            $result=mysqli_query($conn,$sql);
            while($row=mysqli_fetch_array($result)){
                $superID=$row['taskID'];
            }
            $result=mysqli_query($conn,"SELECT task.taskID,ModuleCode,taskName,actualWLUs,unitNumber,isFinished 
            FROM task,allocated_task where task.taskID=allocated_task.taskID and task.taskID=$superID");
            while($row=mysqli_fetch_array($result)){
                $UID="U_".$row['taskID'];
                $FID="F_".$row['taskID'];
                $VID="V_".$row['taskID'];
                echo"   
                    <td>".$row['taskName']."</td>
                    <td>".$row['actualWLUs']."</td>
                    <td><button  type='button'  class='jm-box' data-id='$UID'>Update</button></td>";
                if($row['isFinished']==0){
                     echo"
                        <td>unFinish</td>
                        <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>";
                }
                else{
                    echo"
                        <td>Finished</td>
                        <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>";
                        echo"<td><span class='glyphicon glyphicon-ok success-coin'></span></td>";
                }
            }
            echo"</tbody>
            </table>
            </div>";
            echo"<div class='table-responsive'>
                <table class='table table-striped home-task-table'>
                     <thead>
                    <tr>
                        <th>Task Name</th>
                        <th>Actual WLUs</th>
                        <th>Update</th>
                        <th>Status</th>
                        <th>View</th>
                    </tr>
                    </thead>
                    <tbody>";
            $sql="SELECT task_hierarchy.subtaskID,ModuleCode,taskName,actualWLUs,unitNumber,isFinished 
            FROM workload.task,workload.allocated_task,workload.task_hierarchy 
            where task.taskID=allocated_task.taskID and task.taskID=task_hierarchy.subtaskID and task_hierarchy.taskID='$superID'";
             $result=mysqli_query($conn,$sql);
            while($row=mysqli_fetch_array($result)){
                $UID="U_".$row['subtaskID'];
                $FID="F_".$row['subtaskID'];
                $VID="V_".$row['subtaskID'];
                $subtaskID=$row['subtaskID'];
                $actWLUs=$row['actualWLUs'];
                $unitNum=$row['unitNumber'];
                $totalWorkHours=$actWLUs*$unitNum;
                echo"<tr id='$subtaskID'>
                <td>".$row['taskName']."</td>";
                if($row['actualWLUs']!=null){
                    echo"<td>$totalWorkHours</td>";
                }else{
                    echo"<td></td>";
                }
                echo"<td><button  type='button'  class='jm-box' data-id='$UID'>Update</button></td>";
                if($row['isFinished']==0){//判断是否完成
                    echo"
                        <td><button  type='button'  class='jm-box' data-id='$FID'>Finish</button></td>
                        <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>
                    </tr>";
                }
                else{
                    echo"
                        <td>Finished</td>
                        <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>
                    ";
                    if($row['subtaskID']==$taskID){
                        echo"<td><span class='glyphicon glyphicon-ok success-coin'></span></td>
                        </tr>";
                    }
                }
            }
            echo"</tbody>
            </table>
            </div>";
        }
    }
    function reshow_U($taskID){
        include 'sqlconnect.php';
        $result=mysqli_query($conn,"SELECT task.taskID,ModuleCode,taskName,actualWLUs,unitNumber,isFinished 
        FROM task,allocated_task where task.taskID=allocated_task.taskID and allocated_task.taskID=$taskID");
        while($row=mysqli_fetch_array($result)){
            $UID="U_".$row['taskID'];
            $FID="F_".$row['taskID'];
            $VID="V_".$row['taskID'];
            $taskID=$row['taskID'];
            $actWLUs=$row['actualWLUs'];
            $unitNum=$row['unitNumber'];
            $totalWorkHours=$actWLUs*$unitNum;
            echo"<td>".$row['taskName']."</td>";
            if($row['actualWLUs']!=null){
                echo"<td>$totalWorkHours</td>";
            }else{
                echo"<td></td>";
                }
                echo"<td><button  type='button'  class='jm-box' data-id='$UID'>Update</button></td>";
            if($row['actualWLUs']!=null){//为子任务情况
                if($row['isFinished']==0){//判断是否完成
                     echo"
                        <td><button  type='button'  class='jm-box' data-id='$FID'>Finish</button></td>
                        <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>";
                }
                else{
                    echo"
                        <td>Finished</td>
                        <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>";
                }
            }
            else{//为母任务情况下
                if($row['isFinished']==0){
                     echo"
                        <td>unFinish</td>
                        <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>";
                }
                else{
                    echo"
                        <td>Finished</td>
                        <td><button  type='button'  class='jm-box' data-id='$VID'>View</button></td>";
                }
            }
            echo"<td><span class='success-coin glyphicon glyphicon-ok'></span></td>";
            
        }
    }
    function super_finished($taskID,$current){
        include 'sqlconnect.php';
        $sql="SELECT taskID FROM task_hierarchy WHERE subTaskID='$taskID'";
        if(mysqli_fetch_array(mysqli_query($conn,$sql))){//如果存在母任务
            $sql="SELECT 
            task_hierarchy.taskID, subtaskID, isFinished
            FROM
            allocated_task,task_hierarchy
            WHERE
            allocated_task.taskID = task_hierarchy.subtaskID
            AND task_hierarchy.taskID =(SELECT taskID FROM task_hierarchy WHERE subtaskID='$taskID')";
            $result=mysqli_query($conn,$sql);
            $count=0;
            $superID="";
            while($row=mysqli_fetch_array($result)){
                if($superID==""){
                    $superID=$row['taskID'];
                }
                if($row['isFinished']==0){
                    $count=1;
                }
            }
            if($count==0){//$count=0代表所有子任务都完成了，则母任务完成
                $sql="UPDATE allocated_task SET endTime='$current',isFinished=1 WHERE taskID='$superID'";
                mysqli_query($conn,$sql);
                super_finished($superID,$current);
            }
            else{
                return;
            }

        }
        else{
            return;
        }
    }
?>